/**
 * 
 */
package info.noahcampbell.meter.management.plugin;

import info.noahcampbell.meter.management.reader.RraConfiguredGraph;

import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.jrobin.core.RrdException;
import org.jrobin.graph.RrdGraph;
import org.jrobin.graph.RrdGraphDef;
import org.jrobin.graph.RrdGraphDefTemplate;

/**
 * @author Noah Campbell
 * @version 1.0
 */
public class MeterReaderBean {

    /** The DUR_DISPLAY. */
    public static final String DUR_DISPLAY = "?dur=display";

    /** The DUR_YEAR. */
    public static final String DUR_YEAR = "?dur=year";

    /** The DUR_WEEK. */
    public static final String DUR_WEEK = "?dur=week";

    /** The DUR_DAY. */
    public static final String DUR_DAY = "?dur=day";

    /** The DUR_FIVE. */
    public static final String DUR_FIVE = "?dur=five";
    
    /** The baseUrl. */
    private String baseUrl = "/meter/graph";
    
    /** The graphs. */
    private Map<String, RrdGraph> graphs = new HashMap<String, RrdGraph>(5, 1.0F);
    
    private String id;
    
    public String getPreviewImageUrl() { return baseUrl + MeterReaderBean.DUR_DISPLAY + "psid=" + id; }
    public String get5minImageUrl() { return baseUrl + MeterReaderBean.DUR_FIVE + "psid=" + id; }
    public String get1dayImageUrl() { return baseUrl + MeterReaderBean.DUR_DAY + "psid=" + id; }
    public String get1weekImageUrl() { return baseUrl + MeterReaderBean.DUR_WEEK + "psid=" + id; }
    public String get1yearImageUrl() { return baseUrl + MeterReaderBean.DUR_YEAR + "psid=" + id; }
    
    
    /**
     * @param queryString
     * @return bytes PNG Format
     * @throws IOException
     * @throws RrdException
     */
    public byte[] getImage(String queryString) throws IOException, RrdException {
        return graphs.get(queryString).getPNGBytes();
    }
    
    /**
     * @param templateName
     * @param id 
     * @throws Exception 
     * @throws UnableToFindTemplateException 
     */
    public void setTemplate(String templateName, String id) throws UnableToFindTemplateException, Exception  {
        
        this.id = id;
        
        RraConfiguredGraph graphTemplate = RrdManager.instance().getTemplate(templateName);
        RrdGraphDefTemplate tempalte = new RrdGraphDefTemplate(graphTemplate.getTemplate());
        RrdGraphDef graphDef = tempalte.getRrdGraphDef();
        GregorianCalendar now = new GregorianCalendar();
        
        GregorianCalendar fiveMinutesAgo = (GregorianCalendar) now.clone();
        fiveMinutesAgo.add(GregorianCalendar.MINUTE, -5);
        graphDef.setTimePeriod(fiveMinutesAgo,now);
        graphs.put(MeterReaderBean.DUR_FIVE, new RrdGraph(graphDef));
        
        GregorianCalendar oneDayAgo = (GregorianCalendar) now.clone();
        oneDayAgo.add(GregorianCalendar.HOUR, -24);
        graphDef.setTimePeriod(fiveMinutesAgo,now);
        graphs.put(MeterReaderBean.DUR_DAY, new RrdGraph(graphDef));
        
        GregorianCalendar twoWeeksAgo = (GregorianCalendar) now.clone();
        twoWeeksAgo.add(GregorianCalendar.WEEK_OF_YEAR, -2);
        graphDef.setTimePeriod(twoWeeksAgo,now);
        graphs.put(MeterReaderBean.DUR_WEEK, new RrdGraph(graphDef));
        
        GregorianCalendar oneYearAgo = (GregorianCalendar) now.clone();
        oneYearAgo.add(GregorianCalendar.YEAR, -1);
        graphDef.setTimePeriod(oneYearAgo,now);
        graphs.put(MeterReaderBean.DUR_YEAR, new RrdGraph(graphDef));
        
        graphDef.setTimePeriod(fiveMinutesAgo,now);
        graphDef.setShowLegend(false);
        graphDef.setShowSignature(false);
        graphDef.setChartLeftPadding(10);
        graphs.put(MeterReaderBean.DUR_DISPLAY, new RrdGraph(graphDef));
        
    }
    

}
